package com.atguigu.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class DeadLockDemo {
    static ReentrantLock lock1= new ReentrantLock();
    static ReentrantLock lock2 = new ReentrantLock();
    public static void main(String[] args) {
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t"+"------run over...");
            lock1.lock();
            try {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock2.lock();
                try {
                    System.out.println(Thread.currentThread().getName()+"\t"+"------run over...");
                }finally {
                    lock2.unlock();
                }
            } finally {
                lock1.unlock();
            }
        },"A").start();

        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t"+"------run over...");
            lock2.lock();
            try {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lock1.lock();
                try {
                    System.out.println(Thread.currentThread().getName()+"\t"+"------run over...");
                } finally {
                    lock1.unlock();
                }
            } finally {
                lock2.unlock();
            }
        },"B").start();

    }

}
